﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
    public partial class zg : Form
    {
        Form parent = null;
        int loginname = 0;
        string name = "";
        string nowdate = "";
        dbutils db = new dbutils();

        public void setValue(int loginname, string name, Form f1)
        {
            this.loginname = loginname;
            this.name = name;
            parent = f1;
        }


        public zg()
        {
            InitializeComponent();
        }
        private void loadd1()
        {
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from qj_table order by qjdate DESC";
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            bindingSource1.DataSource = myds.Tables["Bill"];
            dataGridView1.DataSource = bindingSource1;
            dataGridView1.Columns["num"].HeaderText = "编号";
            dataGridView1.Columns["staffid"].HeaderText = "员工编号";
            dataGridView1.Columns["staffname"].HeaderText = "员工姓名";
            dataGridView1.Columns["leavedate"].HeaderText = "请假日期";
            dataGridView1.Columns["qjdate"].HeaderText = "申请日期";
            conn.Close();
        }

        private void loadd2()
        {
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from qj_tg_table order by qj_tg_date DESC";
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            bindingSource2.DataSource = myds.Tables["Bill"];
            dataGridView2.DataSource = bindingSource2;
            dataGridView2.Columns["num"].HeaderText = "编号";
            dataGridView2.Columns["staffid"].HeaderText = "员工编号";
            dataGridView2.Columns["staffname"].HeaderText = "员工姓名";
            dataGridView2.Columns["leavedate"].HeaderText = "请假日期";
            dataGridView2.Columns["qjdate"].HeaderText = "申请日期";
            dataGridView2.Columns["qj_tg_date"].HeaderText = "申请通过日期";
            conn.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text==null)
                return;
            if (textBox1.Text.Trim() == "")
                return;
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from qj_table where num="+textBox1.Text;
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            if (myds.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("编号不存在！");
                conn.Close();
                return;
            }
            string staffid = myds.Tables[0].Rows[0][1].ToString();
            string staffname = myds.Tables[0].Rows[0][2].ToString();
            string leavedate = myds.Tables[0].Rows[0][3].ToString();
            string qjdate = myds.Tables[0].Rows[0][4].ToString();
            conn.Close();
            leavedate=DateTime.Parse(leavedate).ToString("yyyy-MM-dd");
            qjdate = DateTime.Parse(qjdate).ToString("yyyy-MM-dd");
            if (MessageBox.Show("确定批准" + staffname + "于" + leavedate + "的请假申请吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string sql=
                    "insert into qj_tg_table values('"+staffid+"','"+staffname+"','"+leavedate+"','"+qjdate+"','"+nowdate+"',"+textBox1.Text+")";
                string sql1 =
                    "delete from qj_table where num=" + textBox1.Text;
                MessageBox.Show(sql);
                MessageBox.Show(sql1);
                int re1=db.insert(sql) ;
                int re2 = db.insert(sql1);

                sql1 = "select * from kq_month_table where year =" + DateTime.Now.Year.ToString() + " and month=" + DateTime.Now.Month.ToString() + " and staffid='" + loginname + "'";
                if (db.select(sql1) == 0)
                {
                    string sql2 = "insert into kq_month_table values(" + DateTime.Now.Year.ToString() + "," + DateTime.Now.Month.ToString() + ",'" + loginname + "','" + name + "',0,0,0)";
                    if (db.insert(sql2) == 0)
                    {
                        MessageBox.Show("error!");
                        return;
                    }
                }
                string sql3 = "update kq_month_table set leave_num=leave_num+1,tot_num=tot_num+1 where year =" + DateTime.Now.Year.ToString() + " and month=" + DateTime.Now.Month.ToString() + " and staffid='" + loginname + "'";
                int re3 = db.insert(sql3);

                if (re1 > 0&& re2>0&&re3>0)
                {
                    MessageBox.Show("操作成功！"+re1+re2);
                    loadd1();
                    loadd2();
                    return;
                }
                MessageBox.Show("操作失败！" + re1 + re2);

            }
        }

        private void zg_Load(object sender, EventArgs e)
        {
            label4.Text = "您好，主管 " + name;
            nowdate = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
            loadd1();
            loadd2();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from qj_table where num=" + textBox1.Text;
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            if (myds.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("编号不存在！");
                conn.Close();
                return;
            }
            string staffid = myds.Tables[0].Rows[0][1].ToString();
            string staffname = myds.Tables[0].Rows[0][2].ToString();
            string leavedate = myds.Tables[0].Rows[0][3].ToString();
            string qjdate = myds.Tables[0].Rows[0][4].ToString();
            conn.Close();
            leavedate = DateTime.Parse(leavedate).ToString("yyyy-MM-dd");
            qjdate = DateTime.Parse(qjdate).ToString("yyyy-MM-dd");
            if (MessageBox.Show("确定拒绝" + staffname + "于" + leavedate + "的请假申请吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string sql1 =
                    "delete from qj_table where num=" + textBox1.Text;
                //MessageBox.Show(sql);
                MessageBox.Show(sql1);
                //int re1 = db.insert(sql);
                int re2 = db.insert(sql1);
                if (re2 > 0)
                {
                    MessageBox.Show("操作成功！" + re2);
                    loadd1();
                    //loadd2();
                    return;
                }
                MessageBox.Show("操作失败！"  + re2);

            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
